﻿using System;

namespace _1351_count_negative_numbers_in_a_sorted_matrix
{
    class Program
    {
        static void Main (string[] args)
        {
            //int[][] grid = {
            //    new int[] { 4, 3, 2, -1 },
            //    new int[] {3,2,1,-1},
            //    new int[] {1, 1, -1, -2},
            //    new int[] { -1, -1, -2, -3 }
            //};
            int[][] grid = {
                new int[] {5,1,0},
                new int[] { -5, -5, -5 }
            };
            Console.WriteLine (new Solution ().CountNegatives (grid));
        }
    }

    public class Solution
    {
        public int CountNegatives (int[][] grid)
        {
            int count = 0;
            for (int i = grid.Length - 1;i >= 0;i--)
            {
                for (int j = grid[i].Length - 1;j >= 0;j--)
                {
                    if (grid[i][j] >= 0) break;
                    else count++;
                }
            }

            return count;
        }
    }
}
